/**
 * @author kevinlaizhiyu@foxmail.com
 * @description court controller
 * @date 2024-6-6
 * @version 1.0.0
 */

import {NextFunction, Request, Response} from "express";
import CourtsService from "../services/CourtService";
import ResponseDTO from "@/dto/ResponseDTO";

class CourtController {
    /** 创建球场 */
    public async createCourt(req: Request, res: Response, next: NextFunction) {
        try {
            const courtParams = req.body;
            await CourtsService.createCourt(courtParams);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 获取球场列表 */
    public async getCourtList(req: Request, res: Response, next: NextFunction) {
        try {
            const listParams = {
                pageSize: Number(req.query?.pageSize || 10),
                pageIndex: Number(req.query?.pageIndex || 1),
                venueId: String(req.query?.venueId || ""),
                surface: String(req.query?.surface || ""),
            };
            const courtList = await CourtsService.getCourtList(listParams);
            res.locals.data = courtList;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 更新球场信息 */
    public async updateCourt(req: Request, res: Response, next: NextFunction) {
        try {
            const courtParams = req.body;
            if (!courtParams?.courtId) {
                res.send(ResponseDTO.error(100, "参数错误，courtId不能为空"));
                return;
            }
            await CourtsService.updateCourt(courtParams);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 删除球场信息 */
    public async deleteCourt(req: Request, res: Response, next: NextFunction) {
        try {
            const courtId = req.body.courtId;
            if (!courtId) {
                res.send(ResponseDTO.error(100, "参数错误，courtId不能为空"));
            }
            await CourtsService.deleteCourt(courtId as string);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 获取单个球场信息 */
    public async getCourtById(req: Request, res: Response, next: NextFunction) {
        try {
            const courtId = req.query.courtId as string;
            const court = await CourtsService.getCourtById(courtId);
            res.locals.data = court;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }
}

export default new CourtController();
